package DBUnitTests;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import dataBase.Code;
import dataBase.DBInitializer;
import dataBase.Version;
import dataBase.magaers.CodeDBMngr;


public class CodeDBMngrTest {

//	@Test
	public void testAddNewCode() throws SQLException {
		List<String> cat = new LinkedList<String>();
		cat.add("sort");
		Code code = new Code("sort", 1, "java", "bla", "eladKravi@gmail.com", true, cat);
		
		DBInitializer.getInstance().setUp();
		CodeDBMngr.getInstance().addNewCode(code);
	}
	
	@Test
	public void testXMLCreate() throws ParserConfigurationException, TransformerException, SAXException, IOException
	{
		String title = "sort";
		List<Version> versionList = new ArrayList<Version>();
		List<String> categoryList = new ArrayList<String>();
		
		versionList.add(new Version("1", true, "blabla", "java", "elad", "e", "k", false));
		versionList.add(new Version("2", true, "blublu", "c", "yoni", "y", "d", true));
		
		categoryList.add("bubble");
		categoryList.add("trouble");
		

		CodeDBMngr cdbm = CodeDBMngr.getInstance();
		Document doc = cdbm.createDomDocument("e5", title, versionList, categoryList);
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result =  new StreamResult(new File("test.xml"));
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "documents.dtd");
        transformer.transform(source, result);
        
		DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
		dfactory.setValidating(true);
		DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
		Document doc2 = dBuilder.parse("test.xml");
		
		
		
	}
}

